﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OPMS_V1.DOL;
using System.Data;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Net.Mail;

namespace OPMS_V1.BOL
{
    public class Register
    {
        //Initializing the DOL Class associated with it.
        RegisterDOB regObj = new RegisterDOB();
        MailMessage msg = new MailMessage();
        Crypto crypt = new Crypto();

        //Contructor for Register Class
        public Register()
        {}

        /// <summary>
        /// Auto Implemented Properties for Name
        /// </summary>
        public String Name
        {
            get; set;
        }

        /// <summary>
        /// Auto Implemented Properties for Password
        /// </summary>
        public String Password
        {
            get; set;
        }

        /// <summary>
        /// Parameterized Constructor
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Password"></param>
        public Register(String Name, String Password)
        {
            this.Name = Name;
            this.Password = Password;
        }

       
        /// <summary>
        /// This function is used for registering the user to the system.
        /// It registers the user with their account name, email, password, etc.
        /// </summary>
        /// <param name="accountName"></param>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <param name="accountStatus"></param>
        /// <param name="accountType"></param>
        /// <param name="IdentificationId"></param>
        /// <param name="lastLogin"></param>
        /// <returns></returns>
        #region Register User
        public DataSet RegisterUser(String accountName, String email, String password, String accountStatus, String accountType, String IdentificationId, String lastLogin)
        {
            //Registering the User to the Database in case all the Validation is correct
            DataSet condition = regObj.RegisterUser(accountName, email, password, accountStatus, accountType, IdentificationId, lastLogin);
            return condition;
        }
        #endregion Register User


        /// <summary>
        /// This function is used to return all the usernames from the database to check for 
        /// their existence.
        /// </summary>
        /// <returns></returns>
        public List<String> getUsernames()
        {
            //Getting all list of usernames and then checking it for its existance with the form
            List<String> user = new List<string>();
            user = regObj.getUsernames();

            return user;
        }

        /// <summary>
        /// This function is used to return all the email address from the database to check for 
        /// their existence.
        /// </summary>
        /// <returns></returns>
        public List<String> getEmailAddress()
        {
            //Getting all list of email address and then checking it for its existance with the form
            List<String> email = new List<string>();
            email = regObj.getEmailAddress();

            return email;
        }


        #region Validate Users
        /// <summary>
        /// This function is used for Validating the TBD Members with the encrypted Password
        /// </summary>
        /// <param name="Username"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public int ValidateUser(String Username, String Password)
        {
            int condition;
            //Checking for the Boolean value for the condition of their Authorisation
            condition = regObj.LoginUser(Username, Password);
            return condition;
        }
        #endregion Validate Users

        /// <summary>
        /// This function is used for getting the Email Address for the User
        /// incase they forget their password
        /// </summary>
        /// <param name="emailaddress"></param>
        /// <returns></returns>
        public List<string> ForgotUsername(string emailaddress)
        {
            List<string> username=new List<string>();

            username = regObj.ForgotUsername(emailaddress);
            return username;
        }

        /// <summary>
        /// This function is used for getting the password from the database 
        /// incase the user forgets the passwords
        /// </summary>
        /// <param name="emailaddress"></param>
        /// <returns></returns>
        public List<string> ForgotPassword(string emailaddress)
        {
            List<string> password = new List<string>();
            //Returning the encrypted Password from the database
            password = regObj.ForgotPassword(emailaddress);
            return password;
        }

        /// <summary>
        /// This function is used to send the message to the user if exists
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="emailaddress"></param>
        #region Send the Message to the User for the Forgotten Password
        public void SendMessage(String username, String password,string emailaddress)
        {
            string sendusername = username;
            //Decrypting the Password retreived from the database
            string sendpassword = crypt.Decrypt(password);

            try
            {
                //Adding the Information or the message that is to be sent to the User
                msg.To.Add(new MailAddress(emailaddress));
                msg.From = new MailAddress("pgarg1989@gmail.com");
                //Adding the Subject
                msg.Subject = "Member Centre Membership: Password Reminder";
                //Adding the Body of the Message
                msg.Body = "Dear Member, \n\nYou have requested the account details for this email address: " + emailaddress +
                    ". \nBelow is a list of all the accounts linked to this email address:\n\nAccount Login: " + sendusername + "\nPassword: " + sendpassword +
                    "\n\nIf you would like to change the details for any of these accounts, simply visit the TBD Member Centre and log in\nusing the login details above for the account you would like to change. \nIf you would like to cancel an account, please email us at pgarg.aupp@gmail.com." +
                    "\n\nKind regards, \nTBD Membership Services";
                msg.SubjectEncoding = System.Text.Encoding.UTF8;
                msg.BodyEncoding = System.Text.Encoding.UTF8;
                
                /*SmtpClient client = new SmtpClient();
                
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                System.Net.NetworkCredential nc = new System.Net.NetworkCredential(crypt.Decrypt("AjC25N0Lr52z85uJFDtN9Yvz3mhVe8j6U1cUbWEjw7K4jYI5g84be0s7L8RMiUx9"), crypt.Decrypt("gPF0ybaOG1umHDoWzPcOKd+w3qehT+fmyOEzxays0X8="));
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = nc;
                client.Send(msg);*/

                msg.To.Add(new MailAddress(emailaddress));
                msg.From = new MailAddress("s3218012@cs.rmit.edu.au");
                msg.Subject = "Member Centre Membership: Password Reminder";
                msg.Body = "Dear Member, \n\nYou have requested the account details for this email address: " + emailaddress +
                    ". \nBelow is a list of all the accounts linked to this email address:\n\nAccount Login: " + sendusername + "\nPassword: " + sendpassword +
                    "\n\nIf you would like to change the details for any of these accounts, simply visit the TBD Member Centre and log in\nusing the login details above for the account you would like to change. \nIf you would like to cancel an account, please email us at pgarg.aupp@gmail.com." +
                    "\n\nKind regards, \nTBD Membership Services";
                msg.SubjectEncoding = System.Text.Encoding.UTF8;
                msg.BodyEncoding = System.Text.Encoding.UTF8;

                //using smtp client to send the Message
                SmtpClient client = new SmtpClient();
                client.Host = "localhost";
                client.Port = 5587;
                client.Send(msg);
            }
            catch (Exception ex)
            {
                string x = ex.Message;
            }
        }
        #endregion Send the Message to the User for the Forgotten Password        
    }
}
